package a2022.a20221010;

/**
 * @author lenovo
 * @date 2022/10/10
 */
public class a {
    public static void main(String[] args) {
        System.out.println(minSwap(
                new int[]{3,3,8,9,10},
                new int[]{1,7,4,6,8}
        ));
        System.out.println(minSwap2(
                new int[]{3,3,8,9,10},
                new int[]{1,7,4,6,8}
        ));
    }

    public static int minSwap(int[] nums1, int[] nums2) {
        int length = nums1.length;
        int num1 = 0;
//        int num2 = 0;
//        int[] ints1 = Arrays.copyOf(nums1, length);
//        int[] ints2 = Arrays.copyOf(nums2, length);

        for (int i = length-1; i >0; i--) {
            if (nums1[i] <= nums1[i - 1] || nums2[i] <= nums2[i - 1]) {
                int a = i;
                int b = nums1[i+1];
                nums1[i] = nums2[i];
                nums2[i] = b;
                i = a + 1;
                num1++;
            }
        }
//
//        for (int i =0; i <length-1; i++) {
//            if (ints1[i] >= ints1[i + 1] || ints2[i] >= ints2[i + 1]) {
//                int a = i;
//                int b = ints1[i];
//                ints1[i] = ints2[i];
//                ints2[i] = b;
//                i = a - 1;
//                num2++;
//            }
//        }

        return num1;
    }

    public static int minSwap2(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int a = 0, b = 1;
        for (int i = 1; i < n; i++) {
            int at = a, bt = b;
            a = b = n;
            if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1])  {
                a = Math.min(a, at);
                b = Math.min(b, bt + 1);
            }
            if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {
                a = Math.min(a, bt);
                b = Math.min(b, at + 1);
            }
        }
        return Math.min(a, b);
    }

}
